/*
 * Filename ReportConfManageController.java 2022年09月06日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.backend.controller;

import com.ehome.context.thread.UserThreadLocal;
import com.ehome.core.page.PageInfo;
import com.ehome.exception.BizCheckedException;
import com.ehome.poct.backend.api.constant.BackBizCode;
import com.ehome.poct.core.api.constant.enums.ReportAnalysisType;
import com.ehome.poct.backend.api.qry.ReportPermissionPageQry;
import com.ehome.poct.backend.api.qry.ReportPermissionQry;
import com.ehome.poct.backend.api.vo.AddReportPermissionVO;
import com.ehome.poct.backend.api.vo.EditReportPermissionVO;
import com.ehome.poct.backend.api.vo.ReportAnalysisTypeVO;
import com.ehome.poct.backend.fegin.AnalysisPermissionClient;
import com.ehome.poct.backend.mapping.ReporPermissionMapping;
import com.ehome.poct.core.api.dto.report.ConfAnalysisPermissionDTO;
import com.ehome.poct.core.api.vo.report.ConfAnalysisPermissionVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 报告解读配置管理
 *
 * @author wyc
 * @since 2022/09/06 10:56
 **/
@Api( tags = "报告解读配置管理")
@ApiSupport(author = "wyc")
@RestController
@RequiredArgsConstructor
@RequestMapping("/report/conf")
public class ReportConfManageController {
    private final ReporPermissionMapping permissionMapping;
    private final AnalysisPermissionClient permissionClient;

    @ApiOperation("解读权限配置分页查询")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/page")
    public PageInfo<ConfAnalysisPermissionVO> page(ReportPermissionPageQry qry) {
        return permissionClient.pageQuery(qry);
    }

    @ApiOperation("添加解读权限配置")
    @ApiOperationSupport(author = "wyc")
    @PostMapping
    public void save(@RequestBody AddReportPermissionVO add) {
        final var config = permissionClient.queryOne(new ReportPermissionQry().setAnalysisCode(add.getAnalysisCode()));
        if (config != null) {
            throw new BizCheckedException(BackBizCode.ANALYSIS_PERMISSION_EXIST);
        }
        permissionClient.insert(permissionMapping.to(add));
    }

    @ApiOperation("修改解读权限配置")
    @ApiOperationSupport(author = "wyc")
    @PutMapping("/{id}")
    public void update(@PathVariable @ApiParam("权限ID") Long id, @RequestBody EditReportPermissionVO edit) {
        permissionClient.update(id, permissionMapping.to(edit));
    }

    @ApiOperation("启用/禁用")
    @ApiOperationSupport(author = "wyc")
    @PutMapping("/{id}/{status}")
    public void enable(@PathVariable @ApiParam("权限ID") Long id, @PathVariable @ApiParam("状态 0：启用，1：禁用") Integer status) {
        permissionClient.update(id, new ConfAnalysisPermissionDTO().setStatus(status).setLastOperator(UserThreadLocal.getUser().getName()));
    }

    @ApiOperation("查询所有报告内容类型")
    @GetMapping("/type/all")
    public List<ReportAnalysisTypeVO> analysisTypeList() {
        return permissionMapping.to(Arrays.asList(ReportAnalysisType.values()));
    }
}